#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2013-9-5

@author: iori
'''


import os, sys, argparse
from utils import progress_bar
from youtube import YouTube
def cli(argv):
    parser = argparse.ArgumentParser()
    parser.add_argument("-u", '--url', help='video url')
    parser.add_argument('-r', '--resolution', default='480p', help='resolution to download, like 420p, 720p')
    parser.add_argument('-e', '--extension', default='mp4', help='video format like mp4, flv, webm ')
    parser.add_argument('-p', '--path', default='.', help='where to store the video')
    parser.add_argument('-f', '--file', help="load url from files , one url per line")
    args = parser.parse_args(argv)
    yt = YouTube()
    if args.url:
        yt.url = args.url
        v= yt.get(args.extension, args.resolution)
        if v:
            v.download(on_progress=progress_bar)
    elif args.file:
        if os.path.isfile(args.file):
            urls = None
            with open(args.file, 'rb') as fp:
                urls = fp.read().split('\n')
            for url in urls:
                yt.url = url
                v = yt.get(args.extension, args.resolution)
                v.download(on_progress=progress_bar)
def main():
    cli(sys.argv[1:])
    
if __name__ == '__main__':
    main()